<?php
require_once '../libs/app.config.php';
session_start ();
if ($_SESSION ['LoginId'] == false) {
	header ( "Location:login.php" );
}
$tpn = new Page ();
// 分类列表
$sql = "select id, orderId, origin, destination, items, status, createTime from orders where status = 'ongoing' order by createTime desc, orderId desc";
$query = $tpm->query ( $sql );
$totals = $tpm->num_rows ( $query );
$tpn->pageft ( $totals, 10 );
$result = $tpm->query ( $sql . " limit " . $tpn->getfirstcount () . ",10" );

$sql4 = "select id, value_type, value_info from setting_value where value_type = 'location' order by value_info";
$query4 = $tpm->query ( $sql4 );
while ( $rows = $tpm->fetch_array ( $query4 ) ) {
	$locations [] = array (
			'id' => $rows ['id'],
			'value_info' => $rows ['value_info'] 
	);
}

$sql5 = "select id, value_type, value_info from setting_value where value_type = 'status' order by value_info";
$query5 = $tpm->query ( $sql5 );
while ( $rows = $tpm->fetch_array ( $query5 ) ) {
	$status [] = array (
			'id' => $rows ['id'],
			'value_info' => $rows ['value_info'] 
	);
}

$getTime = date ( "Y/m/d h:i:s ", time () );

while ( $rows = $tpm->fetch_array ( $result ) ) {
	$order_list [] = array (
			'id' => $rows ['id'],
			'orderId' => $rows ['orderId'],
			'origin' => $rows ['origin'],
			'destination' => $rows ['destination'],
			'items' => $rows ['items'],
			'status' => $rows ['status'] 
	);
}

// 多项删除
if ($_POST ['del']) {
	if (! empty ( $_POST ['check'] )) {
		$array = $_POST ['check'];
		$arr = implode ( ',', $array );
		$id = explode ( ',', $arr );
		$size = count ( $id );
		for($j = 0; $j < $size; $j ++) {
			$sql1 = "delete from orders where orderId= '" . $id [$j] . "'";
			$sql2 = "delete from order_records where orderId = '" . $id [$j] . "'";
			$query2 = $tpm->query ( $sql2 );
			$query1 = $tpm->query ( $sql1 );
			
			if ($query2 > 0 && $query1 > 0)
				echo "<script>alert('Order deletion is completed');location.href=('ongoing_orders_list.php');</script>";
		}
	}
	else
		echo "<script>Please check orders first!');history.back();</script>";
}

// 多项Complete
if ($_POST ['complete']) {
	if (! empty ( $_POST ['check'] )) {
		$array = $_POST ['check'];
		$arr = implode ( ',', $array );
		$id = explode ( ',', $arr );
		$size = count ( $id );
		$completedNo = 0;
		$failedNo = 0;
		for($j = 0; $j < $size; $j ++) {
			$compleTime = date ( "Y/m/d h:i:s ", time () );
			$sql = "update orders set status = 'completed', completeTime = sysdate()  where orderId = '" . $id [$j] . "'";
			$query = $tpm->query ( $sql );
			
			if ($query > 0)
				$completedNo = $completedNo + 1;
			else
				$failedNo = $failedNo + 1;
		}
		echo "<script>alert('Total {$completedNo} orders completed successfully \\nTotal {$failedNo} orders failed!');location.href=('ongoing_orders_list.php');</script>";
	
	}
	else
		echo "<script>Please check orders first!');history.back();</script>";
}

// 多项添加record
if ($_POST ['add']) {
	$addedNo = 0;
	$failedNo = 0;
	if (! empty ( $_POST ['check'] )) {
		$array = $_POST ['check'];
		$arr = implode ( ',', $array );
		$id = explode ( ',', $arr );
		$size = count ( $id );
		$formattedTime = (strlen ( $datepicker ) > 12) ? $datepicker : (substr ( $datepicker, 6, 4 ) . "-" . substr ( $datepicker, 0, 2 ) . "-" . substr ( $datepicker, 3, 2 ));
		
		for($j = 0; $j < $size; $j ++) {
			$add = array (
					"orderId" => $id [$j],
					"time" => $formattedTime,
					"location" => $location,
					"detail" => $detail 
			);
			$result = $tpm->insert ( "order_records", $add );
			if ($result > 0)
				$addedNo = $addedNo + 1;
			else
				$failedNo = $failedNo + 1;
		}
		echo "<script>alert('Total {$addedNo} orders added a record successfully \\nTotal {$failedNo} orders failed!');location.href=('ongoing_orders_list.php');</script>";
	}
	else
		echo "<script>Please check orders first!');history.back();</script>";
}

// 单项删除
if ($_POST ["delid"]) {
	$sql = "select distinct orderId from orders where id = '" . $_POST ["delid"] . "'";
	$query = $tpm->query ( $sql );
	$rows = $tpm->fetch_array ( $query );
	$sql = "delete from orders where id= '" . $_POST ['delid'] . "'";
	$sql2 = "delete from order_records where orderId = '" . $rows ['orderId'] . "'";
	$query2 = $tpm->query ( $sql2 );
	$query1 = $tpm->query ( $sql );
	if ($tpm->affected_rows ( $query2 ) && $tpm->affected_rows ( $query1 ))
		echo "1";
	else
		echo "0";
}

if ($_POST ['completeId']) {
	$compleTime = date ( "Y/m/d h:i:s ", time () );
	$sql = "update orders set status = 'completed', completeTime = sysdate()  where id = '" . $_POST ['completeId'] . "'";
	$query = $tpm->query ( $sql );
	
	if ($query > 0)
		echo "<script>alert('Order: {$searchId} is completed successfully!');location.href=('ongoing_orders_list.php');</script>";
}

require_once (INCLUDE_PATH . 'template.inc.php'); // 模板类及对象$tpl的加载
$tpl->assign ( "locations", $locations );
$tpl->assign ( "status", $status );
$tpl->assign ( "order_list", $order_list );
$tpl->assign ( "getTime", $getTime );
$tpl->assign ( 'pages', $tpn->getpagenav () );
$tpl->display ( "ongoing_orders_list.html" );
?>